package com.fowo.api.mp.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 公众号模版消息
 */
@Data
public class WxTemplateMessage {

    public WxTemplateMessage(String templateId, String toUser) {
        this.templateId = templateId;
        this.toUser = toUser;
    }

    public WxTemplateMessage addData(String name, String value, String color) {
        final TemplateData templateData = new TemplateData();
        templateData.setValue(value);
        templateData.setColor(color);
        this.data.put(name, templateData);
        return this;
    }

    /**
     * 接收者（用户）的 openid
     */
    @JsonProperty("touser")
    private String toUser;

    /**
     * 所需下发的订阅模板id
     */
    @JsonProperty("template_id")
    private String templateId;

    /**
     * 模板跳转链接（海外帐号没有跳转能力）
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String url;

    /**
     * 跳小程序所需数据，不需跳小程序可不用传该数据
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Miniprogram miniprogram;

    /**
     * 模板数据
     */
    private Map<String,TemplateData> data = new ConcurrentHashMap<>();

    @Data
    public static class TemplateData {
        private String value;

        @JsonInclude(JsonInclude.Include.NON_NULL)
        private String color;
    }

    @Data
    public static class Miniprogram {
        private String appid;

        @JsonInclude(JsonInclude.Include.NON_NULL)
        private String pagepath;
    }
}
